package entity.general;

import java.io.Serializable;

public class MinTime implements Serializable, Comparable <MinTime>, Time {
	int hours = 0, minutes = 0;
	
	public MinTime(int h, int m)
	{
		if(minutes >= 60)
			throw new RuntimeException("Minutes " + minutes + " > 60");
//		if(hours > 24)
//			throw new RuntimeException("Hours " + hours + " > 24");
//		if(hours == 24 && minutes != 0)
//			throw new RuntimeException("Hours is 24, minutes is " + minutes + " > 0");
			
		hours = h;
		minutes = m;
	}
	
	public MinTime(int minutes) {
		this.minutes = minutes;
		hours = this.minutes / 60;
		this.minutes %= 60;
//		seconds = (seconds + 30) % 60 * 60;
//		System.out.println("Seconds " + seconds);
//		hours = seconds / 60 / 60;
//		System.out.println("Hours " + hours);
//		minutes = seconds / 60 % 60;
//		System.out.println("Minutes " + minutes);
	}

	public int getMinutes()
	{
		return minutes;
	}
	
	public int getHours()
	{
		return hours;
	}

	@Override
	public int compareTo(MinTime arg0) {
		if(hours > arg0.getHours())
			return 1;
		else if(hours < arg0.getHours())
			return -1;
		else
		{
			if(minutes > arg0.getMinutes())
			{
				return 1;
			}
			else if(minutes < arg0.getMinutes())
			{
				return -1;
			}
		}
		return 0;
	}

	public MinTime add(MinTime time) {
		int nminutes = time.minutes + minutes;
		int nhours = time.hours + hours;
		
		nhours = nhours + (nminutes / 60);
		nminutes %= 60;
		
		return new MinTime(nhours, nminutes);
	}	

	public int hashCode()
	{
		return minutes + hours * 60;
	}
	
	public boolean equals(Object o)
	{
		if(o instanceof MinTime)
		{
			MinTime ot = (MinTime)o;
			if(ot.getHours() == hours && ot.getMinutes() == minutes)
			{
				return true;
			}
		}
		return false;
	}
	
	public String toString()
	{
		String str = "";
		if(hours == 0)
		{
			str += "00:";
		}
		else if(hours < 10)
		{
			str += "0" + hours +  ":";			
		}
		else
		{
			str += hours +  ":";						
		}
		
		if(minutes == 0)
		{
			str += "00";
		}
		else if(minutes < 10)
		{
			str += "0" + minutes;			
		}
		else
		{
			str += minutes;						
		}
		
		return str;
	}

	public int getAsMinutes() {
		return minutes + hours * 60;
	}

	public MinTime multBy(float f) {
		int minutes = getAsMinutes();
		minutes = (int) (minutes * f);
		return new MinTime(minutes);
	}

	public MinTime sub(MinTime time) {
		int mins = this.getAsMinutes() - time.getAsMinutes();
		if(mins < 0)
			mins = 0;
		return new MinTime(mins);
	}
}
